package view.rules.components;

import java.awt.Component;
import java.awt.Dimension;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.JScrollPane;

import net.miginfocom.swing.MigLayout;
import managers.FeatureManager;
import features.DBFeatureValues;
import features.Feature;


public class FeaturePanel extends JPanel {
	
	private JTable table;
	private JScrollPane scrollPane;
	private String pos;
	private ArrayList<Feature> features;
	private String [] featureTypes;
	private Object[][] featureNames ;
	private int biggestSize;
	private JPanel tablePanel;
	private selectedCheckBox  listener;
	private String[][] selectedValues;
	Map<String,ArrayList<String>> multiMap;
	

	public FeaturePanel() {
		this.setLayout(new MigLayout());
		initComponents();
		addComponents();
	}
	public FeaturePanel(String pos) {
		this.setLayout(new MigLayout());
		this.pos = pos;
		System.out.println("Feature panel " + this.pos);
		initComponents(pos);
		addComponents();
	}

	public void initComponents(String pos) {
		//System.out.println(pos + "*&*&*&*&*&*&*&*&*&&*&&&*");
		listener = new selectedCheckBox();
		features = FeatureManager.getDefaultFeatures(pos);
		featureTypes = new String[features.size()*2];
		biggestSize = getSize(features, pos);
		featureNames = new Object [biggestSize][features.size()*2];

		tablePanel = new JPanel();
		tablePanel.setLayout(new MigLayout());
		
		int k = 0;
		
		for(int i = 0; i < (features.size() * 2); i++){
			if(i % 2 == 0){
				featureTypes[i] = "Boolean";
				for(int j = 0; j < biggestSize; j++){
					featureNames[j][i] = false;
				}
			} 
			else{
				featureTypes[i] = features.get(k).getName(); 
				DBFeatureValues values = FeatureManager.getFeatureValues(pos, features.get(k).getName());
				for(int j = 0; j < values.getValues().size(); j++){
					featureNames[j][i] = values.getValues().get(j);
				}
				k++;
			}
		}
	
		DefaultTableModel model = new DefaultTableModel(featureNames, featureTypes);
		table = new JTable(model){
			  
		    @Override
            public Class getColumnClass(int column) {
                if(column % 2 == 0)
                	return Boolean.class;
                else
                	return String.class;
            }
		    
		    @Override
	         public Component prepareRenderer(TableCellRenderer renderer, int row,int column) {
	            Component component = super.prepareRenderer(renderer, row, column);
	            int rendererWidth = component.getPreferredSize().width;
	            TableColumn tableColumn = getColumnModel().getColumn(column);
	            tableColumn.setPreferredWidth(Math.max(rendererWidth +
	                    getIntercellSpacing().width,
	                    tableColumn.getPreferredWidth()));
	            return  component;
	         }
		    
		    @Override
		    public boolean isCellEditable(int row, int col) {
		    	
		    	if (col % 2 == 1)
		    		return false;
		    	else
		    		return true;
		    }
		    
		};
		table.addMouseListener(listener);
		table.setPreferredScrollableViewportSize(table.getPreferredSize());
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		table.setFocusable(false);
		table.setRowSelectionAllowed(false);
		scrollPane = new JScrollPane(table);
		scrollPane.setPreferredSize(new Dimension(700, 700));
		tablePanel.add(scrollPane);
	}
	
	
	public void initComponents() {
		listener = new selectedCheckBox();
		features = FeatureManager.getDefaultFeatures("N");
		featureTypes = new String[features.size()*2];
		biggestSize = getSize(features, "N");
		featureNames = new Object [biggestSize][features.size()*2];

		tablePanel = new JPanel();
		tablePanel.setLayout(new MigLayout());
		
		int k = 0;
		
		for(int i = 0; i < (features.size() * 2); i++){
			if(i % 2 == 0){
				featureTypes[i] = "Boolean";
				for(int j = 0; j < biggestSize; j++){
					featureNames[j][i] = false;
				}
			} 
			else{
				featureTypes[i] = features.get(k).getName(); 
				DBFeatureValues values = FeatureManager.getFeatureValues("N", features.get(k).getName());
				for(int j = 0; j < values.getValues().size(); j++){
					featureNames[j][i] = values.getValues().get(j);
				}
				k++;
			}
		}
	
		DefaultTableModel model = new DefaultTableModel(featureNames, featureTypes);
		table = new JTable(model){
			  
		    @Override
            public Class getColumnClass(int column) {
                if(column % 2 == 0)
                	return Boolean.class;
                else
                	return String.class;
            }
		    
		    @Override
	         public Component prepareRenderer(TableCellRenderer renderer, int row,int column) {
	            Component component = super.prepareRenderer(renderer, row, column);
	            int rendererWidth = component.getPreferredSize().width;
	            TableColumn tableColumn = getColumnModel().getColumn(column);
	            tableColumn.setPreferredWidth(Math.max(rendererWidth +
	                    getIntercellSpacing().width,
	                    tableColumn.getPreferredWidth()));
	            return  component;
	         }
		    
		    @Override
		    public boolean isCellEditable(int row, int col) {
		    	
		    	if (col % 2 == 1)
		    		return false;
		    	else
		    		return true;
		    }
		    
		};
		table.addMouseListener(listener);
		table.setPreferredScrollableViewportSize(table.getPreferredSize());
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		table.setFocusable(false);
		table.setRowSelectionAllowed(false);
		scrollPane = new JScrollPane(table);
		scrollPane.setPreferredSize(new Dimension(700, 700));
		tablePanel.add(scrollPane);
	}
	
	private void addComponents(){
		add(tablePanel, "wrap");
	}
	
	public int getSize(ArrayList<Feature> features, String pos){
		int bigSize = 0;
		
		for(int i = 0; i < features.size(); i++){
			DBFeatureValues values = FeatureManager.getFeatureValues(pos, features.get(i).getName());
			if(values.getValues() == null)
				System.out.println(values.getValues());
			if(bigSize < values.getValues().size()){
				bigSize = values.getValues().size();
			}
		}
		return bigSize;
	}

	public String getPos() {
		return pos;
	}

	public void setPos(String pos) {
		this.pos = pos;
	}
	
	public void setValue(int row, int column){
		if(featureNames[row][column].equals(true))
			featureNames[row][column] = false;
		else
			featureNames[row][column] = true;
	}
	
	public Map<String,ArrayList<String>>  getSelected(){
		ArrayList<String> values;
		multiMap = new HashMap<String,ArrayList<String>>();
		
		for(int i = 0; i < features.size()*2; i++){
			if(i % 2 == 0){
				values = new ArrayList<String>();
				for(int j = 0; j < biggestSize; j ++){
					if(featureNames[j][i].equals(true))
						values.add((String)featureNames[j][i+1]);
				}
				
				if(!(values.isEmpty()))
					multiMap.put(table.getColumnName(i+1), values);
			}
		}
		
	
		
		return multiMap;
	}

	private class selectedCheckBox implements MouseListener {

		@Override
		public void mouseClicked(MouseEvent e) {
		
			int row = table.rowAtPoint(e.getPoint());
			int column = table.columnAtPoint(e.getPoint());
			
			setValue(row, column);
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

	
	}
}
